package com.ruoyi.web.controller.api.suggest;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.system.domain.PAnswer;
import com.ruoyi.system.domain.PQuestion;
import com.ruoyi.system.domain.bo.PQuestionBo;
import com.ruoyi.system.service.IPAnswerService;
import com.ruoyi.system.service.IPQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 问答-发布问题api
 * @Author: wk
 * @Date: 2022/7/19 8:31
 * @Version: 1.0
 */
@Api(value = "问题控制器", tags = {"前端-问题管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/question")
public class QuestionApiController extends BaseController {
    private final IPQuestionService iPQuestionService;
    private final IPAnswerService answerService;

    @ApiOperation("发布问题分页列表")
    @GetMapping("/get-page-list")
    public TableDataInfo<PQuestion> list(@RequestParam("search") String search,
                                         @RequestParam("typeId") Long typeId,
                                         @RequestParam("userType") String userType,
                                         @RequestParam("end") String end,
                                         PageQuery pageQuery) {
        return iPQuestionService.queryPageListafter(search, typeId, userType, end, pageQuery);
    }

    /**
     * 获取问题详细信息
     */
    @ApiOperation("获取问题详细信息")
    @GetMapping("/detail/{id}")
    public R<Map<String, Object>> getInfo(@ApiParam("主键")
                                  @NotNull(message = "主键不能为空")
                                  @PathVariable("id") Long id) {
        Map<String, Object> res = new HashMap<>();
        res.put("question", iPQuestionService.queryClickById(id));
        res.put("answer", answerService.queryListByQid(id));
        return R.ok(res);
    }

    /**
     * 新增问题
     */
    @ApiOperation("新增问题")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PQuestionBo bo) {
        return toAjax(iPQuestionService.insertByUserBo(bo) ? 1 : 0);
    }

    /**
     * 新增问题
     */
    @ApiOperation("新增问答回复")
    @PostMapping("/addAnswer")
    public R<PAnswer> addAnswer(@RequestBody PAnswer answer) {
        return R.ok(this.answerService.addAnswer(answer));
    }

    /**
     * 修改问题
     */
    @ApiOperation("修改问题")
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PQuestionBo bo) {
        return toAjax(iPQuestionService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除问题
     */
    @ApiOperation("删除问题")
    @PostMapping("/del/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPQuestionService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("问答详情-》问题")
    @GetMapping("/detailQuestion")
    public R<PQuestion> detailQuestion(@RequestParam("questionId") Long questionId) {
        return R.ok(this.iPQuestionService.queryById(questionId));
    }

    @ApiOperation("问答详情-》答案")
    @GetMapping("/detailAnswer")
    public TableDataInfo<PAnswer> detailAnswer(@RequestParam("questionId") Long questionId, PageQuery pageQuery) {
        return this.answerService.detailAnswer(questionId, pageQuery);
    }

    @ApiOperation("问题发布")
    @PostMapping("/addQuestion")
    public R<Void> addQuestion(@RequestBody PQuestion pQuestion) {
        this.answerService.addQuestion(pQuestion);
        return R.ok();
    }

    @ApiOperation("更新阅读量")
    @GetMapping("/updateReadNum")
    public R<Void> updateReadNum(@RequestParam("questionId") Long questionId) {
        this.iPQuestionService.updateReadNum(questionId);
        return R.ok();
    }
}
